package iws.gui;

import iws.g5.uebung3.Simulator;
import iws.g5.uebung3.StateModel;
import iws.g5.uebung3.enums.OrganicType;
import iws.g5.uebung3.gui.Display;
import iws.g5.uebung3.gui.GuiConsole;
import iws.g5.uebung3.interfaces.OrganicObject;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class DrawingPanel extends JPanel implements Display {
	private static final long serialVersionUID = 1L;
	private StateModel env;
	int i=0;
	public DrawingPanel() {
		new Thread(){
			public void run() {
				Simulator sim = new Simulator(DrawingPanel.this);
				sim.simulate();
			};
		}.start();
	}
	
	@Override
	public void paint(Graphics g) {
		//super.paint(g);
		//int width = this.getWidth()/(env.getXDimension());
		//int height = this.getHeight()/(env.getYDimension());
		int width = 2;
		int height = 2;
		
		for(int y=0;y<env.getYDimension();y++){
			for(int x=0;x<env.getXDimension();x++){
				OrganicObject object=env.getOrganicObject(x, y);
				if(object==null){
					g.setColor(Color.BLACK);
				}else if(object.getType()==OrganicType.CELL){
					g.setColor(Color.GREEN);
				}else if(object.getType()==OrganicType.VIRUS){
					g.setColor(Color.RED);
				}else{
					g.setColor(Color.CYAN);
				}
				
				g.fillRect(x*width, y*height, width, height);
				
			}
		}
	}

	@Override
	public void drawEnvironment(StateModel env) {
		this.env = env;
		this.repaint();
	}
}
